<template>
  <div id="app">
      <router-view style="margin-right: 45px;" v-if="isRouterAlive"></router-view>
         <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="original">
          <el-input  maxlength="20" type='password' placeholder="请输入原密码(6-20个字符,不包含空格和中文,必须包含数字,字母或字符至少两种！)" v-model.number="ruleForm2.original"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input  maxlength="20" type="password"  placeholder="请输入新密码(6-20个字符,不包含空格和中文,必须包含数字,字母或字符至少两种！)" v-model="ruleForm2.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')" :loading='loading'>确定</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
 import evBus from '@/utils/bus';
import { updatePassword } from '@/api/administrator';
  export default{
    name: 'App',
    provide() {
      return {
        reload: this.reload
      }
    },
     data () {
    var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入原密码'));
        }
          var isNum = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\s\u4e00-\u9fa5]{5,19}$/;
          if(!isNum.test(value)){
               callback(new Error('6-20个字符,不包含空格,不能包含中文,必须包含数字,字母或字符至少两种!'));
          }

        callback()
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
           var isNum = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^[^\s\u4e00-\u9fa5]{5,19}$/;
          if(!isNum.test(value)){
               callback(new Error('6-20个字符,不包含空格,不能包含中文,必须包含数字,字母或字符至少两种!'));
          }
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      dialogFormVisible: false,
      ruleForm2: {
          pass: '',
          checkPass: '',
          original: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          original: [
            { validator: checkAge, trigger: 'blur' }
          ]
      },
      loading: false,
      isRouterAlive: true
    }
  },
  mounted () {
    evBus.$on('updatePassword', () => {
      this.dialogFormVisible = true
    })
    
    console.log('%c当前版本v2.0.0,更新版本时间2022/8/1 17:30',"color:green;font-size:18px;")
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nestTick(function () {
        this.isRouterAlive = true
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate( async (valid) => {
        if (valid) {
          this.loading = true
          try {
          const res = await updatePassword({
            oldPassWord: this.ruleForm2.original,
            passWord: this.ruleForm2.pass
          })
          this.loading = false
          if (res.code > 0) {
            this.$message({
              type: 'success',
              message: '修改成功，需要重新登录，2秒后跳转登录页面'
            })
            this.dialogFormVisible = false
            setTimeout(() => {
              this.$store.dispatch('FedLogOut').then(() => {
                location.reload()// In order to re-instantiate the vue-router object to avoid bugs
              })
            }, 2000);
           
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            })
          }
          console.log(res);
          }catch(error){
            this.loading = false
          }
        } else {
          this.$message({
            type: 'warning',
            message: '请填写完整的表单'
          })
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
  }
</script>
